#!/usr/bin/env python
# coding=utf-8
"""WTFM"""


def romain_to_int(a):
    Symbols = {
        'M': 1000,
        'D': 500,
        'C': 100,
        'L': 50,
        'X': 10,
        'V': 5,
        'I': 1,
    }

    result = 0
    i = 0
    while True:
        capacity = Symbols[a[i]]
        if i == len(a) - 1:
            result += capacity
            break
        else:
            nextCap = Symbols[a[i + 1]]
            if nextCap > capacity:
                result -= capacity
            else:
                result += capacity
            i = i + 1
    return result


if __name__ == '__main__':
    print(romain_to_int('CMLXXXVII'))
